package com.hskn.hss.module.attendkqjg.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.attendkqjg.entity.AttendKqjg;
import com.hskn.hss.module.attendkqjg.mapper.AttendKqjgMapper;
import com.hskn.hss.module.attendkqjg.service.IAttendKqjgService;
import com.hskn.hss.module.rectifyrecord.entity.RectifyRecord;
import com.hskn.hss.module.rectifyrecord.mapper.RectifyRecordMapper;
import com.hskn.hss.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-06-07
 */
@Service
public class AttendKqjgServiceImpl extends ServiceImpl<AttendKqjgMapper, AttendKqjg> implements IAttendKqjgService {

    @Resource
    AttendKqjgMapper attendKqjgMapper;
    @Resource
    RectifyRecordMapper rectifyRecordMapper;

    @Override
    @Transactional
    public void updateOrSave(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        String account = LoginUtils.getUser().getAccount();
        if (StringUtils.isEmpty(params) ||
                (StringUtils.isEmpty(params.getString("attendKqjgId")) && StringUtils.isEmpty(params.getString("attendId")))) {
            throw new HssException("参数错误");
        }
        AttendKqjg attendKqjg = JSON.parseObject(params.toJSONString(), AttendKqjg.class);
        attendKqjg.setUpdateUid(uid);
        saveOrUpdate(attendKqjg);
        RectifyRecord rectifyRecord = JSON.parseObject(params.toJSONString(), RectifyRecord.class);
        rectifyRecord.setRectifyUid(uid);
        rectifyRecord.setRectifyName(account);
        rectifyRecord.setSbdkjgJzh(attendKqjg.getSbdkjg());
        rectifyRecord.setSbdksjJzh(attendKqjg.getSbdksj());
        rectifyRecord.setXbdkjgJzh(attendKqjg.getXbdkjg());
        rectifyRecord.setXbdksjJzh(attendKqjg.getXbdksj());
        rectifyRecordMapper.insert(rectifyRecord);
    }

    @Override
    public void update(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("attendKqjgId"))) {
            throw new HssException("参数错误");
        }
        AttendKqjg attendKqjg = JSON.parseObject(params.toJSONString(), AttendKqjg.class);
        attendKqjg.setUpdateUid(uid);
        attendKqjgMapper.updateById(attendKqjg);
    }

    @Override
    public JSONObject mList(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        JSONObject json = new JSONObject();
        QueryWrapper<AttendKqjg> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(AttendKqjg::getCreateTime);
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        Page<AttendKqjg> attendKqjgPage = new Page<>(page, size, true);
        IPage<AttendKqjg> iPage = attendKqjgMapper.selectPage(attendKqjgPage, queryWrapper);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("list", iPage.getRecords());
        return json;
    }

}
